<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* UGCWasm (https://iclient.supermap.io/web/libs/ugcwasm/1.0.1/UGCWasmAll.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_ugcParallelAndPerpendicular"></title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        background: #fff;
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        width: 280px;
        text-align: center;
        z-index: 10000;
        border-radius: 4px;
      }
      .ol-popup-close-button {
        outline: none;
      }
    </style>
  </head>

  <body>
    <div id="toolbar" class="panel panel-primary">
      <div class="panel-heading">
        <h5 class="panel-title text-center">线线关系</h5>
      </div>
      <div class="panel-body content">
        <div class="panel">
          <div class="input-group">
            <span class="input-group-addon">位置</span>
            <select id="positon" class="form-control">
              <option value="isParallel">平行</option>
              <option value="isSegmentIntersect">相交</option>
            </select>
          </div>
        </div>
        <input type="button" class="btn btn-default" value="查找" onclick="search()" />&nbsp;
        <input type="button" class="btn btn-default" value="移除" onclick="clearLayer()" />
      </div>
    </div>
    <div id="map" style="width: 100%; height: 100%"></div>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" include="ugcwasm" src="../../dist/ol/include-ol.js"></script>
    <script>
      var map,
        targetLine,
        resultLine,
        lineLists = [];
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var url = host + '/iserver/services/map-world/rest/maps/World';
      ol.supermap.initMap(url, {
        viewOptions: {
          center: [120, 30],
          zoom: 6
        }
      })
        .then((res) => {
          map = res.map;
          addFeatures();
        });

      function addFeatures() {
        targetLine = {
          type: 'Feature',
          geometry: {
            type: 'LineString',
            coordinates: [
              [116, 30],
              [128, 30]
            ]
          }
        };
        const line2 = {
          type: 'Feature',
          geometry: {
            type: 'LineString',
            coordinates: [
              [116, 28],
              [128, 28]
            ]
          }
        };
        const line3 = {
          type: 'Feature',
          geometry: {
            type: 'LineString',
            coordinates: [
              [120, 26],
              [120, 33]
            ]
          }
        };
        const line4 = {
          type: 'Feature',
          geometry: {
            type: 'LineString',
            coordinates: [
              [121, 27],
              [125, 34]
            ]
          }
        };
        const line5 = {
          type: 'Feature',
          geometry: {
            type: 'LineString',
            coordinates: [
              [121, 31],
              [124, 33]
            ]
          }
        };

        searchLines = [line2, line3, line4, line5];
        addLayer(targetLine, {
          color: 'blue'
        });
        addLayer(searchLines, {
          color: 'green'
        });
      }

      function search() {
        clearLayer();
        var geometryAnalysis = new ol.supermap.GeometryAnalysis();
        var positonFun = document.getElementById('positon').value;
        var searchResults = [];
        var [[x1, y1], [x2, y2]] = targetLine.geometry.coordinates;
        searchLines.forEach((line) => {
          var [[x3, y3], [x4, y4]] = line.geometry.coordinates;
          var booleanResult = geometryAnalysis[positonFun](x1, y1, x2, y2, x3, y3, x4, y4);
          if (booleanResult) {
            searchResults.push(line);
          }
        });
        resultLine = addLayer(searchResults, {
          color: 'red'
        });
      }

      function clearLayer() {
        if (!map || !resultLine) {
          return;
        }
        map.removeLayer(resultLine);
      }

      function addLayer(geojson, style = {}) {
        var features = [];
        if (Array.isArray(geojson)) {
          geojson.forEach((item) => {
            features.push(new ol.format.GeoJSON().readFeature(item))
          })
        } else {
          features = [new ol.format.GeoJSON().readFeature(geojson)];
        } 
        var source = new ol.source.Vector({
          features: features,
          wrapX: false
        });
        var layer = new ol.layer.Vector({
          source: source,
          style: new ol.style.Style({
            image: new ol.style.Circle({
              radius: 8,
              fill: new ol.style.Fill({
                color: style.color || 'coral'
              })
            }),
            stroke: new ol.style.Stroke({
              color: style.color || 'blue',
              width: 3
            })
          })
        });
        map.addLayer(layer);
        return layer;
      }
    </script>
  </body>
</html>
